1148B - Born This Way - CodeForces Solution


binary search brute force two pointers *1600

Please click on ads to support us..

Python Code:

n,m,ta,tb,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
for i in range(n):
    a[i]+=ta
i = 0
MaxTime = -1
j = 0
while i<n and j<m:
    if b[j]>=a[i]:
        if k>0:
            k-=1
            i+=1
        else:
            MaxTime = b[j]+tb
            break
    j+=1
print(MaxTime)

C++ Code:

#include <bits/stdc++.h>

#define _PRINT_ 1
#define _SOLVE_ 1

#if defined(DEBUG) && _PRINT_ == 1
#include "debug.h"
#else
#define print(x, ...) 0
#define debug(x, ...) 0
#define verify(x, ...) 0
#endif

using namespace std;

using ll = long long;
constexpr int inf = 1e9 + 1;
constexpr ll llinf = 1e18 + 1;

/**
 *
 */
void solve() {
    int n, m, ta, tb, k;
    cin >> n >> m >> ta >> tb >> k;
    vector<int> a(n), b(m);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }

    vector<int> c(m);
    int i = 0, j = 0;
    while (j < m) {
        if (i == n) {
            c[j++] = n;
            continue;
        }
        if (a[i] + ta <= b[j]) {
            i++;
        } else {
            c[j++] = i;
        }
    }

    auto f = [&](int x)->bool {
        if (x <= k) {
            return true;
        }
        for (int i = 0; i < x; i++) {
            if (c[i] + x - i - 1 <= k) {
                return true;
            }
        }
        return false;
    };

    int l = 0, h = m;
    while (l <= h) {
        int x = (l + h) / 2;
        if (f(x)) {
            l = x + 1;
        } else {
            h = x - 1;
        }
    }
    if (h == m) {
        cout << -1;
    } else {
        cout << b[h] + tb;
    }

}

int main() {
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
//    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1717B - Madoka and Underground Competitions
61B - Hard Work
959B - Mahmoud and Ehab and the message
802G - Fake News (easy)
1717C - Madoka and Formal Statement
420A - Start Up
1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies